﻿/* 
 * Copyright 2010 Justin Pealing
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

using System;
using log4net;
using Minnow.Web;
using log4net.Config;
using Minnow.Web.Http;

namespace MinnowConsole
{
    /// <summary>
    /// Sample web server using Minnow.Web
    /// </summary>
    internal class Program
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
        private const string Usage = @"
MinnowConsole.exe
A small sample web server application to demonstrate Minnow web server.

Usage
    Minnow [localroot]

Parameters
    [localroot]
        Optional, specifies the local directory to serve files from.
        Defaults to the current directory

Example
    Minnow c:\MinnowRoot

Starts a web server serving up files from the c:\MinnowRoot directory.

See the source code for more details and configuration options.
";

        static int Main(string[] args)
        {
            string localRoot;

            if (args.Length > 1)
            {
                Console.WriteLine(Usage);
                return 1;
            }
            else if (args.Length == 1)
            {
                switch (args[0].Trim().ToLower())
                {
                    case "?":
                    case "help":
                    case "h":
                        Console.WriteLine(Usage);
                        return 1;
                    default:
                        localRoot = args[0];
                        break;
                }
            }
            else
            {
                localRoot = Environment.CurrentDirectory;
            }

            return internalMain(localRoot);
        }

        private static int internalMain(string LocalRoot)
        {
            WebServer server = new WebServer();
            server.LocalRoot = LocalRoot;
            server.Prefixes.Add("http://+:80/Temporary_Listen_Addresses/");
            server.RootUrl = "/Temporary_Listen_Addresses/";

            AspNetHandler aspHandler = new AspNetHandler();
            server.HandlerMappings.Add(new HandlerMapping("*.aspx", LocalMappingState.None, aspHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.svc", LocalMappingState.None, aspHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.asmx", LocalMappingState.None, aspHandler));

            StaticHandler staticHandler = new StaticHandler();
            server.HandlerMappings.Add(new HandlerMapping("*.css", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.js", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.gif", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.ico", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.png", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.html", staticHandler));
            server.HandlerMappings.Add(new HandlerMapping("*.txt", staticHandler));

            DummyHandler dummyHanlder = new DummyHandler();
            server.HandlerMappings.Add(new HandlerMapping("*.dummy", LocalMappingState.None, dummyHanlder));

            FastCGIHandler cgiHandler = new FastCGIHandler();
            cgiHandler.CommandLine = @"C:\php\php-cgi.exe";
            server.HandlerMappings.Add(new HandlerMapping("*.php", cgiHandler));

            DirectoryHandler directoryHandler = new DirectoryHandler();
            server.HandlerMappings.Add(new HandlerMapping("*", LocalMappingState.Folder, directoryHandler));

            LogManager.GetRepository().RendererMap.Put(typeof(ResponseEventArgs), new ResponseRenderer());
            BasicConfigurator.Configure();
            //server.ProcessingRequest += new EventHandler<ProcessingRequestEventArgs>(server_ProcessingRequest);
            server.HandlerException += new EventHandler<ExceptionEventArgs>(server_HandlerException);
            server.Response += new EventHandler<ResponseEventArgs>(server_Response);
            server.Start();

            Console.WriteLine("Server running. Press enter key to stop...");
            Console.ReadLine();
            return 0;
        }

        static void server_Response(object sender, ResponseEventArgs e)
        {
            logger.Info(e);
        }

        static void server_HandlerException(object sender, ExceptionEventArgs e)
        {
            logger.Error(null, e.Exception);
        }

        static void server_ProcessingRequest(object sender, ProcessingRequestEventArgs e)
        {
            logger.Info(e);
        }
    }
}